package com.pshcp.patient.api;

import com.external.common.WebServiceException;
import com.external.service.WsRequestService;
import com.external.ws.soap.entity.res.HosFeeViewRes;
import com.external.ws.soap.entity.res.OutFeeViewRes;
import com.external.ws.soap.entity.res.PatFees;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.pshcp.domain.model.Patient;
import com.pshcp.patient.domain.searchfee.ao.FeeListAO;
import com.pshcp.patient.domain.searchfee.ao.HosFeeDetailAO;
import com.pshcp.patient.domain.searchfee.ao.OutFeeDetailAO;
import com.pshcp.patient.domain.searchfee.vo.HosFeeDetailVO;
import com.pshcp.patient.domain.searchfee.vo.OutFeeDetailVO;
import com.pshcp.patient.domain.searchfee.vo.PatFeeVO;
import com.pshcp.patient.tool.SearchFeeTools;
import com.pshcp.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = {"患者端-查询-费用查询"})
@Slf4j
@RestController
@RequestMapping("/api/patient/searchFee")
public class SearchFeeAPI {

    @Autowired
    private WsRequestService wsRequestService;

    @Autowired
    private PatientService patientService;

    /**
     * 使用测试数据
     */
    @Value("${ws.test:false}")
    private Boolean hisTest;

    @ApiOperation("费用记录")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "费用记录")
    @PostMapping("/outFeeList")
    public ResultDTO<List<PatFeeVO>> outFeeList(@RequestBody FeeListAO listAO) throws BizException {

        Patient patient = patientService.findById(listAO.getPatientId());
        CommonTool.checkObj(patient, "10180001");

        List<PatFeeVO> patFeeVOS = null;
        try {
            List<PatFees> patFeesList = wsRequestService.feesListQuery(
                    SearchFeeTools.buildFeeListAO2WsDTO(listAO, patient.getHisPatientId())
            );
            patFeeVOS = CommonTool.copyPropertiesList(patFeesList, PatFeeVO.class);
        }catch (WebServiceException e){
            log.error("ws feesListQuery err", e);
        }

        if(hisTest){
            patFeeVOS = SearchFeeTools.buildTestData(listAO.getFeeType());
        }
        return ResultBuilder.success(patFeeVOS);
    }
    @ApiOperation("门诊费用明细")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "门诊费用明细")
    @PostMapping("/outFeeDetail")
    public ResultDTO<OutFeeDetailVO> outFeeDetail(@RequestBody OutFeeDetailAO detailAO) throws BizException {

        Patient patient = patientService.findById(detailAO.getPatientId());
        CommonTool.checkObj(patient, "10180001");

        OutFeeViewRes viewRes = wsRequestService.outFeeList(SearchFeeTools.buildOutFee(detailAO, patient));
        OutFeeDetailVO detailVO = SearchFeeTools.buildWsOutFeeDetail2VO(viewRes);
        if(hisTest){
            detailVO = SearchFeeTools.buildOutFeeDetailTestData();
        }
        return ResultBuilder.success(detailVO);
    }

    @ApiOperation("住院费用明细")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "住院费用明细")
    @PostMapping("/hosFeeDetail")
    public ResultDTO hosFeeDetail(@RequestBody HosFeeDetailAO detailAO) throws BizException {

        Patient patient = patientService.findById(detailAO.getPatientId());
        CommonTool.checkObj(patient, "10180001");

        HosFeeViewRes viewRes = wsRequestService.hosFeeList(SearchFeeTools.buildHosFee(detailAO, patient));
        HosFeeDetailVO detailVO = SearchFeeTools.buildWsHosFeeDetail2VO(viewRes);

        if(hisTest){
            detailVO = SearchFeeTools.buildHosFeeDetailTestData();
        }
        return ResultBuilder.success(detailVO);
    }

}
